package com.ophiux.cs.application.user.impl.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ophiux.cs.application.core.Result;
import com.ophiux.cs.application.core.ResultEnum;
import com.ophiux.cs.application.user.dto.D_GetDoctorUserInfoRequest;
import com.ophiux.cs.application.user.dto.D_GetDoctorUserInfoResponse;
import com.ophiux.cs.domain.user.model.entity.User;
import com.ophiux.cs.domain.user.model.valueobj.User_DoctorApi;
import com.ophiux.cs.domain.user.remote.IUser_DoctorRemoteService;
import com.ophiux.cs.domain.user.repositories.IUserRepository;
import com.ophiux.utils.common.ShiftUtils;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class GetDoctorUserInfoService {
	@Autowired
	IUserRepository iUserRepository;
	@Autowired
	IUser_DoctorRemoteService user_DoctorRemoteService;
	
	public Result<D_GetDoctorUserInfoResponse> getDoctorUserInfo(D_GetDoctorUserInfoRequest dto) {
		Result<D_GetDoctorUserInfoResponse> result = new Result<D_GetDoctorUserInfoResponse>();
		D_GetDoctorUserInfoResponse res = new D_GetDoctorUserInfoResponse();
		try {
			if (!dto.isParamValid()) {
				return Result.fail(ResultEnum.requestParamFail); 
			}
			
			User user = iUserRepository.getUserByUserId(dto.getUSER_ID());
			res = (D_GetDoctorUserInfoResponse) ShiftUtils.objToNewObj(user, D_GetDoctorUserInfoResponse.class);
			if(user==null) {
				return Result.fail(ResultEnum.NotFundUser); 
			}
			
			//调用医生远程api  获取医生信息
			User_DoctorApi apiResult = user_DoctorRemoteService.getDoctorInfoByDoctorId(user.getOAUTH_ID());
			if(apiResult!=null) {
				res.setTECHNICAL_TITLE(apiResult.getTECHNICAL_TITLE());
				res.setCERTIFICATE_NO(apiResult.getCERTIFICATE_NO());
				res.setHOSPITAL_NAME(apiResult.getHOSPITAL_NAME());
				res.setDEPT_NAME(apiResult.getDEPT_NAME());
			}
			
			result = Result.success(res);
			
		} catch (Exception e) {
			log.info("getUserByUserid接口异常：",e);
			e.printStackTrace();
			return Result.fail(ResultEnum.requestException);
		}
		return result;
	}
}
